package com.luluteam.pushlib.tools;

import android.util.Log;

import com.luluteam.pushlib.constant.AppHolder;
import com.luluteam.pushlib.constant.WebConstants;
import com.luluteam.pushlib.model.ContextInfo;
import com.luluteam.pushlib.model.DeviceInfo;
import com.luluteam.pushlib.model.PushLogin;
import com.luluteam.pushlib.model.PushReps;
import com.luluteam.pushlib.util.SharedPreferencesUtil;
import com.luluteam.pushlib.util.okhttp.OkHttpManager;

import org.json.JSONObject;

;

/**
 * @author Guan
 * @date Created on 2018/11/21
 */
public class Repository {
    private static final String TAG = "Repository";


    /**
     * TODO 上传采集的情景信息
     *
     * @param info
     */
    public static void postContextInfo(ContextInfo info) {

        Log.e(TAG, "上传采集的情景信息");
        OkHttpManager.postJson(WebConstants.ADD_CONTEXT_INFO, info.toJson(), new OkHttpManager.ResultCallback() {
            @Override
            public void onSuccess(String result) {
                Log.e(TAG, result);
            }
        });

    }

    /**
     * 上报设备信息
     *
     * @param deviceInfo
     */
    public static void postDeviceInfo(DeviceInfo deviceInfo) {
        Log.i(TAG, "上报设备信息");
        OkHttpManager.postJson(WebConstants.PUSH_DEVICE, deviceInfo.toJson(), new OkHttpManager.ResultCallback() {
                    @Override
                    public void onSuccess(String result) {
                        SharedPreferencesUtil.putBoolean("hasUploaded", true);
                    }
                }
        );
    }

    /**
     * 向服务器发送登录信息
     *
     * @param pushLogin
     */
    public static void postPushLogin(PushLogin pushLogin) {
        OkHttpManager.postJson(WebConstants.PUSH_LOGIN, pushLogin.toJson(), new OkHttpManager.ResultCallback() {
            @Override
            public void onSuccess(String result) {
                Log.i(TAG, "登录信息回调。。。。。。" + result);
            }
        });
    }

    /**
     * 向服务器上报，表示已经成功接收到推送的消息
     *
     * @param pushReps
     */
    public static void postPushReceive(PushReps pushReps) {
        OkHttpManager.postJson(WebConstants.PUSH_RECEIVE, pushReps.toJson(), new OkHttpManager.ResultCallback() {
            @Override
            public void onSuccess(String result) {
                Log.i(TAG, "已经成功接收Msg，向服务器上报确认");
            }
        });
    }


    /**
     * 记录对收到的消息的操作信息
     *
     * @param pushReps
     */
    public static void postPushReps(PushReps pushReps) {
        OkHttpManager.postJson(WebConstants.PUSH_RESPONSE, pushReps.toJson(), new OkHttpManager.ResultCallback() {
            @Override
            public void onSuccess(String result) {
                Log.i(TAG, "已经对消息进行了操作，上报服务器");
            }
        });
    }

    /**
     * 获取离线小子
     *
     * @param callback
     */
    public static void getOfflineMsg(OkHttpManager.ResultCallback callback) {
        try {
            JSONObject jsonObject = new JSONObject();
            jsonObject.put("appId", AppHolder.APP_ID);
            jsonObject.put("deviceId", AppHolder.deviceId);
            OkHttpManager.postJson(WebConstants.OFFLINE_MESSAGE, jsonObject.toString(), callback);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
